// *** LICENSE HEADER ***
// Filename: ./resource.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "resource.h"

SDL_Color resource::lightBlue = SDL_Color();

GLdouble resource::projectionMatrix[16] = {0.0, 0.0, 0.0, 0.0,
                                           0.0, 0.0, 0.0, 0.0,
                                           0.0, 0.0, 0.0, 0.0,
                                           0.0, 0.0, 0.0, 0.0};
GLdouble resource::transformMatrix[16] = {0.0, 0.0, 0.0, 0.0,
                                          0.0, 0.0, 0.0, 0.0,
                                          0.0, 0.0, 0.0, 0.0,
                                          0.0, 0.0, 0.0, 0.0};
GLint resource::viewportMatrix[4] = {0, 0, 0, 0};

resource::resource(float posX, float posY, float radius, int ammount, game* newGame){
    x = posX;
    y = posY;
    r = radius;
    amnt = ammount;

    myLight = 0;
    myEmitter = -1;

    theGame = newGame;

    next = 0;
}

resource::~resource() {
    if (myLight)
        theGame->GetLightManager()->RemoveLight(myLight);
    if (myEmitter != -1)
        theGame->GetParticleSystem()->RemoveEmitter(myEmitter);
}

void resource::DrawRadius(){
    glColor3f(0.35f, 0.35f, 0.35f);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x + r, y + r, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x + r, y - r, 0.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x - r, y - r, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x - r, y + r, 0.0f);
    glEnd();
}

void resource::SetNext(resource* other){
    next = other;
}

resource* resource::GetNext(){
    return next;
}

int resource::GetType(){
    return myType;
}

int resource::GetAmmount(){
    return amnt;
}

float resource::GetX(){
    return x;
}

float resource::GetY(){
    return y;
}

void resource::SetColor(){
    lightBlue.r = 164;
    lightBlue.g = 190;
    lightBlue.b = 255;
}

void resource::GetMatrices(){
    //Get the matrices from opengl and store them in the global buffers
    glGetDoublev(GL_MODELVIEW_MATRIX, transformMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
    glGetIntegerv(GL_VIEWPORT, viewportMatrix);
}

resourceAttack::resourceAttack(float posX, float posY, float radius, int ammount, game* newGame):
    resource(posX, posY, radius, ammount, newGame) {
    //Set the correct emitter, light and resource type
    myEmitter = theGame->GetParticleSystem()->CreateEmitter(x, y, 0.23f, 0, _PET_FIRE_IMMORTAL);
    myLight = theGame->GetLightManager()->AddLight(0.5f, 0.1f, 0.0f, posX, posY, radius*4);
    myType = RT_ATTACK;

    //Set the helper buffer
    sprintf(buffer, "%i#(att.)", ammount);
}

void resourceAttack::Grow(node* pl){
    pl->growthAmmount[RT_ATTACK] += amnt;
    pl->growthWave[RT_ATTACK] = 1;
}

void resourceAttack::Draw(fonts* fontViewer, bool drawAmmounts){
    //Set correct light params and enable lighting
    GLfloat lightParams[4] = {1.0f, 0.8f, 0.2f, 0.0f};
    GLfloat lightPos[4] = {x, y, 0.3f, 0.0f};
    glLightfv(0, GL_SPECULAR, lightParams);
    glLightfv(0, GL_DIFFUSE, lightParams);
    glLightfv(0, GL_POSITION, lightPos);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glBindTexture(GL_TEXTURE_2D, resourceList::textureAttack);
    glTranslatef(x, y, 0.0f);
    #include "attackglcode.h"
    glTranslatef(-x, -y, 0.0f);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);

    //Draw the text, informing about the resource ammount
    if (drawAmmounts){
        GLdouble winX, winY, winZ;
        GLint out = gluProject(x, y, 1.0f, transformMatrix, projectionMatrix, viewportMatrix, &winX, &winY, &winZ);
        if (winX > 0 && winX < _WIDTH && winY > 0 && winY < _HEIGHT)
            fontViewer->Print(FT_MENU, buffer, (int)(winX), (int)(winY), 10, lightBlue);
    }
}

resourceDefence::resourceDefence (float posX, float posY, float radius, int ammount, game* newGame):
    resource(posX, posY, radius, ammount, newGame) {
    //Set the correct emitter, light and resource type
    myEmitter = theGame->GetParticleSystem()->CreateEmitter(x, y, 0.21f, 1, _PET_SPHERE_IMMORTAL);
    myLight = theGame->GetLightManager()->AddLight(0.0f, 0.5f, 0.2f, posX, posY, radius*4);
    myType = RT_DEFENCE;

    //Set the helper buffer
    sprintf(buffer, "%i#(def.)", ammount);
}

void resourceDefence::Grow(node* pl){
    pl->growthAmmount[RT_DEFENCE] += amnt;
    pl->growthWave[RT_DEFENCE] = 1;
}

void resourceDefence::Draw(fonts* fontDrawer, bool drawAmmounts){
    //Set correct light params and enable lighting
    GLfloat lightParams[4] = {0.0f, 0.1f, 0.5f, 0.0f};
    GLfloat lightPos[4] = {x, y, 0.3f, 0.0f};
    glLightfv(0, GL_SPECULAR, lightParams);
    glLightfv(0, GL_DIFFUSE, lightParams);
    glLightfv(0, GL_POSITION, lightPos);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glBindTexture(GL_TEXTURE_2D, resourceList::textureDefence);
    glTranslatef(x, y, 0.0f);
    #include "defenceglcode.h"
    glTranslatef(-x, -y, 0.0f);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);

    //Draw the text, informing about the resource ammount
    if (drawAmmounts){
        GLdouble winX, winY, winZ;
        GLint out = gluProject(x, y, 0.75f, transformMatrix, projectionMatrix, viewportMatrix, &winX, &winY, &winZ);
        if (winX > 0 && winX < _WIDTH && winY > 0 && winY < _HEIGHT)
            fontDrawer->Print(FT_MENU, buffer, (int)(winX), (int)(winY), 10, lightBlue);
    }
}

resourceLives::resourceLives (float posX, float posY, float radius, int ammount, game* newGame):
    resource(posX, posY, radius, ammount, newGame) {
    //Set the correct emitter, light and resource type
    myEmitter = theGame->GetParticleSystem()->CreateEmitter(x, y, 0.05f, 0, _PET_FOUNTAIN);
    myLight = theGame->GetLightManager()->AddLight(0.0f, 0.25f, 0.5f, posX, posY, radius*4);
    myType = RT_LIVES;

    //Set the helper buffer
    sprintf(buffer, "%i#(lives)", ammount);
}

void resourceLives::Grow(node* pl){
    pl->growthAmmount[RT_LIVES] += amnt;
    pl->growthWave[RT_LIVES] = 1;
}

void resourceLives::Draw(fonts* fontDrawer, bool drawAmmounts){
    //Set correct light params and enable lighting
    GLfloat lightParams[4] = {0.8f, 0.2f, 0.9f, 0.0f};
    GLfloat lightPos[4] = {x, y, 0.3f, 0.0f};
    glLightfv(0, GL_SPECULAR, lightParams);
    glLightfv(0, GL_DIFFUSE, lightParams);
    glLightfv(0, GL_POSITION, lightPos);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glBindTexture(GL_TEXTURE_2D, resourceList::textureLives);
    glTranslatef(x, y, 0.0f);
    #include "livesglcode.h"
    glTranslatef(-x, -y, 0.0f);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);

    //Draw the text, informing about the resource ammount
    if (drawAmmounts){
        GLdouble winX, winY, winZ;
        GLint out = gluProject(x, y, 0.6f, transformMatrix, projectionMatrix, viewportMatrix, &winX, &winY, &winZ);
        if (winX > 0 && winX < _WIDTH && winY > 0 && winY < _HEIGHT)
            fontDrawer->Print(FT_MENU, buffer, (int)(winX), (int)(winY), 10, lightBlue);
    }
}

resourceCredits::resourceCredits (float posX, float posY, float radius, int ammount, game* newGame):
    resource(posX, posY, radius, ammount, newGame) {
    //Set the correct emitter, light and resource type
    myEmitter = theGame->GetParticleSystem()->CreateEmitter(x, y, 0.0f, 2, _PET_DOLLARS);
    myLight = theGame->GetLightManager()->AddLight(0.5f, 0.4f, 0.0f, posX, posY, radius*4);
    myType = RT_CREDITS;

    //Set the helper buffer
    sprintf(buffer, "%i#($$$)", ammount);
}

void resourceCredits::Grow(node* pl){
    pl->growthAmmount[RT_CREDITS] += amnt;
    pl->growthWave[RT_CREDITS] = 1;
}

void resourceCredits::Draw(fonts* fontDrawer, bool drawAmmounts){
    //Set correct light params and enable lighting
    GLfloat lightParams[4] = {0.8f, 0.6f, 0.0f, 0.0f};
    GLfloat lightPos[4] = {x, y, 0.3f, 0.0f};
    glLightfv(0, GL_SPECULAR, lightParams);
    glLightfv(0, GL_DIFFUSE, lightParams);
    glLightfv(0, GL_POSITION, lightPos);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glBindTexture(GL_TEXTURE_2D, resourceList::textureCredits);
    glTranslatef(x, y, 0.0f);
    #include "creditsglcode.h"
    glTranslatef(-x, -y, 0.0f);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);

    //Draw the text, informing about the resource ammount
    if (drawAmmounts){
        GLdouble winX, winY, winZ;
        GLint out = gluProject(x, y, 0.60f, transformMatrix, projectionMatrix, viewportMatrix, &winX, &winY, &winZ);
        if (winX > 0 && winX < _WIDTH && winY > 0 && winY < _HEIGHT)
            fontDrawer->Print(FT_MENU, buffer, (int)(winX), (int)(winY), 10, lightBlue);
    }
}

resourceTakeover::resourceTakeover (float posX, float posY, float radius, int ammount, game* newGame):
    resource(posX, posY, radius, ammount, newGame) {
    //Set the correct emitter, light and resource type
    myEmitter = theGame->GetParticleSystem()->CreateEmitter(x, y, 0.5f, 1, _PET_PSIBURST_IMMORTAL, x, y, 0.25f);
    myLight = theGame->GetLightManager()->AddLight(0.4f, 0.1f, 0.5f, posX, posY, radius*4);
    myType = RT_TAKEOVER;

    //Set the helper buffer
    sprintf(buffer, "%i#(psi)", ammount);
}

void resourceTakeover::Grow(node* pl){
    pl->growthAmmount[RT_TAKEOVER] += amnt;
    pl->growthWave[RT_TAKEOVER] = 1;
}

void resourceTakeover::Draw(fonts* fontDrawer, bool drawAmmounts){
    //Set correct light params and enable lighting
    GLfloat lightParams[4] = {0.8f, 0.2f, 0.9f, 0.0f};
    GLfloat lightPos[4] = {x, y, 0.3f, 0.0f};
    glLightfv(0, GL_SPECULAR, lightParams);
    glLightfv(0, GL_DIFFUSE, lightParams);
    glLightfv(0, GL_POSITION, lightPos);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glBindTexture(GL_TEXTURE_2D, resourceList::textureTakeover);
    glTranslatef(x, y, 0.0f);
    #include "takeoverglcode.h"
    glTranslatef(-x, -y, 0.0f);
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);

    //Draw the text, informing about the resource ammount
    if (drawAmmounts){
        GLdouble winX, winY, winZ;
        GLint out = gluProject(x, y, 0.75f, transformMatrix, projectionMatrix, viewportMatrix, &winX, &winY, &winZ);
        if (winX > 0 && winX < _WIDTH && winY > 0 && winY < _HEIGHT)
            fontDrawer->Print(FT_MENU, buffer, (int)(winX), (int)(winY), 10, lightBlue);
    }
}
